perm filename TUTORI.PL[LIB,PL] blob sn#620883 filedate 1983-12-05 generic text, type T, neo UTF8
/* This file contains all the examples that appear in the prolog tutorial,
<F.PROLOG>TUTORI.LPT

It is divided into screens of 24 lines, so it can be used in a tutorial or
a demo.  It can be consulted, and all the examples in it can be tried.  */

















/* reachability in a graph */



reachable(X,X).
reachable(X,Z) :- edge(X,Y), reachable(Y,Z).

edge(a,b).
edge(b,c).
edge(b,d).
edge(d,b).











/* list processing */


member(X,[X|←]).
member(X,[←|L]) :- member(X,L).


duplicate(X,L1,L2) :- member(X,L1), member(X,L2).


append([],L,L).
append([X|L1],L2,[X|L3]) :- append(L1,L2,L3).



intersect([],←,[]).
intersect([X|L1],L2,[X|L3]) :- member(X,L2), intersect(L1,L2,L3).





/* biblical data base */

father(abraham,isaac).
father(isaac,jacob).
father(isaac,esau).
father(jacob,reuben).
father(jacob,simon).

mother(sarah,isaac).
mother(rebecca,jacob).
mother(rebecca,esau).

parent(X,Y) :- father(X,Y).
parent(X,Y) :- mother(X,Y).

brother(X,Y) :- parent(Z,X), parent(Z,Y), X\==Y.

had←sex(X,Y) :- father(X,Z), mother(Y,Z).

uncle(X,Y) :- father(Z,Y), brother(X,Z).

grandmother(X,Y) :- mother(X,Z), parent(Z,Y).
/* global variables */

set(Name, Value) :-  retractall(variable(Name,←)),
                     assert(variable(Name, Value)).

add1(Name, NewValue) :-  retract(variable(Name, Value)),
                         NewValue is Value + 1,
                         assert(variable(Name, NewValue)).

/* stacks */

emptystack(Name) :- retractall(stack(Name, ←)),
                    assert(stack(Name, [])).

push(Name, Element) :- retract(stack(Name, List)),
                       assert(stack(Name, [Element | List])).

pop(Name, Element) :- retract(stack(Name, [Element | List])).
                      assert(stack(Name, List)).



/* A Prolog interpreter */


execute(true) :- !.
execute((P,Q)) :- !, execute(P), execute(Q).
execute(P) :- clause(P,Q), execute(Q).
















/* Associative assoc in Prolog */

assoc(A,L,S) :- bagof(B,( member([A,B],L) ; member([B,A],L) ),S).

list([[a,b],[a,c],[c,d],[d,a],[d,b]]).

















/* implementing an environment for Prolog */

trace(P) :- asserta((P :- trace, fail)).


notrace(P) :- retract((P :- trace, fail)).
















mcsam(Story) :-  write('Processing story '),  nl, list←print(Story),
                 find(Story, Script, Defaults),
                 process(Script, Story),
                 name←defaults(Defaults),
                 nl, write('The instantiated script is: '), nl,
                 list←print(Script), !.

find(Story, Script, Defaults) :- filler(Slot, Story), suggest(Slot,Script←name),
                                script(Script←name, Script, Defaults),
                                write('Found script: '), write(Script←name), nl.

process(Script, []).
process([Line | Script], [Line | Story]) :-  write(processing←line(Line)), nl,
                                             process(Script, Story).
process([Line | Script], Story) :- process(Script, Story).

filler(Slot, Story) :- member([← | Args], Story),
                       member(Slot, Args), nonvar(Slot).

name←defaults([]).
name←defaults([[N,N]|L]) :-  name←defaults(L).
name←defaults([[←,←]|L]) :-  name←defaults(L).
suggest(leones, restaurant).
suggest(waiter, restaurant).



story([[ptrans, john, john, ←, leones],
       [mtrans, ←, ←, hamburger],
       [ptrans, Actor, Actor, ←, ←]  ]).

script(restaurant,
       [ [ptrans, Actor, Actor, Earlier←place, Restaurant],
         [ptrans, Actor, Actor, Door, Seat],
         [mtrans, Actor, Waiter, Food],
         [atrans, Actor, Money, Actor, Waiter],
         [ptrans, Actor, Actor, Restaurant, Gone] ],
       [ [Actor, customer], [Earlier←place, place1], [Restaurant, restaurant],
         [Door, door], [Seat, seat], [Food, meal], [Waiter, waiter],
         [Money, check], [Gone, place2]  ] ).


list←print([]) :- nl.
list←print([X | L]) :- display(X), nl, list←print(L).

% A program for automatically creating public declarations from mode
% declarations.
public(File1,File2) :-
        see(File1),
        tell(File2),
        write(':- public'),
        public1,
        write('.'),
        close(File1),
        close(File2).

public1 :-
        read(Term),
        ( Term=end←of←file, ! ;
          ( Term=(:-mode Mode),
                functor(Mode,Functor,Arity),
                nl, write('         '),
                write(Functor/Arity),
                write(',') ;
            true ), !,
          public1 ).

% Scribe indexing Program,
% every occurance "W" in Input of a word that is in WordsFile
% is replaced by "W @index(W)" in file  Output
:-public(index/3).
:-mode getword(-),putindex(+),separator(+).

index(Input,Output,WordsFile) :-
        see(WordsFile),
        initialize,
        see(Input),
        tell(Output),
        transform,
        told.

initialize :-
        read(Word),
        ( Word==end←of←file, ! ;
          name(Word,List),
          assert(index(List,Word)),
          !, initialize ).

transform :-
        getword(Word),
        putindex(Word),
        !, transform.

getword(Word) :-
        get0(N), put(N),
        ( separator(N), !, Word=[] ;
          Word=[N|Rest], getword(Rest) ), !.

putindex([]) :- !.
putindex(List) :-
        call(index(List,Word)), !,
        write('@index('),
        write(Word),
        write(')').
putindex(List).

separator(N) :- N<65, !.
separator(N) :- N>122, !.
separator(N) :- N>90, N<97.